package nl.nickthissen.remindme.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nl.nickthissen.remindme.R;
import nl.nickthissen.remindme.activities.EditReminderItemActivity;
import nl.nickthissen.remindme.activities.MainActivity;
import nl.nickthissen.remindme.common.Utils;
import nl.nickthissen.remindme.data.Category;
import nl.nickthissen.remindme.data.ReminderItem;

/**
 * Created by Nick on 11-6-13.
 */
public class CategoryAdapter extends ArrayAdapter<Category>
{
    private ArrayList<Category> items;

    public CategoryAdapter(Context context, int resourceId, ArrayList<Category> items)
    {
        super(context, R.layout.category_row, R.id.lblName, items);
        this.items = items;
    }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent)
    {
        return getCustomView(position, convertView, parent, true);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        return getCustomView(position, convertView, parent, false);
    }

    private View getCustomView(int position, View convertView, ViewGroup parent, boolean dropdown)
    {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.category_row, null);
        }

        Category item = items.get(position);
        if (item != null)
        {
            LinearLayout colorLayout = (LinearLayout) v.findViewById(R.id.colorLayout);
            TextView lblName = (TextView) v.findViewById(R.id.lblName);

            colorLayout.setBackgroundColor(item.color);
            lblName.setText(item.name);

            if (dropdown)
            {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 25, 0, 25);
                lblName.setLayoutParams(params);
            }
        }

        return v;
    }

}
